package com.yeyks.commonReference.append.other;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.RandomUtil;
import com.yeyks.common.exception.ServiceException;
import com.yeyks.commonReference.append.redis.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.ByteArrayOutputStream;
import java.util.Objects;

/**
 * @author shuai
 * @date 2019/12/02 13:38
 */
@Component
public class ImageCodeService {

    private static final String operations = "+-";
    public final String key = "CODE:OPERATION_IMAGE_CODE:";

    @Autowired
    private RedisService redisService;

    //圈验证码对象
    private CircleCaptcha captcha = null;
    //线验证码对象
//    private final LineCaptcha captcha = CaptchaUtil.createLineCaptcha(100, 35, 4, 40);

    @PostConstruct
    public void init() {
        captcha = CaptchaUtil.createCircleCaptcha(160, 60, 4, 0);
        //初始化图片颜色
        captcha.setBackground(new Color(235, 235, 235));
    }

    /**
     * 获取验证码图片
     *
     * @param type  {@link com.yeyks.common.constants.code.ImageCodeTypeConstant}
     * @param phone 手机号
     */
    public String getCode(String type, String phone) {
        try {
            String imgCode = getImgCode(type, phone);
            Image image = captcha.createImage(imgCode);
            return ImgUtil.toBase64(image, ImgUtil.IMAGE_TYPE_PNG);
        } catch (Exception e) {
            e.printStackTrace();
        }
        throw new ServiceException("图片验证码生成失败");
    }

    /**
     * 获取验证码图片
     *
     * @param type  {@link com.yeyks.common.constants.code.ImageCodeTypeConstant}
     * @param phone 手机号
     */
    public void getCode(String type, String phone, HttpServletResponse response) {
        try {
            String imgCode = getImgCode(type, phone);
            Image image = captcha.createImage(imgCode);
            final ByteArrayOutputStream out = new ByteArrayOutputStream();
            ImgUtil.writePng(image, out);
            IoUtil.write(response.getOutputStream(), false, out.toByteArray());
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("图片验证码生成失败");
        }
    }

    /**
     * 校验验证码
     *
     * @param phone 手机号
     * @param code  验证码
     */
    public Boolean verify(String type, String phone, String code) {
        try {
            String value = redisService.getValue(key + type + ":" + phone);
            redisService.deleteKey(key + type + ":" + phone);
            return Objects.equals(value, code);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 获取图片code
     *
     * @return
     */
    private String getImgCode(String type, String phone) {
        int begin = RandomUtil.randomInt(1, 20);
        String randomOperation = RandomUtil.randomString(operations, 1);
        int end = RandomUtil.randomInt(1, 20);
        int result = 0;
        if ("-".equals(randomOperation)) {
            if (begin < end) {
                int temp = begin;
                begin = end;
                end = temp;
            }
            result = begin - end;
        }
        if ("+".equals(randomOperation)) {
            result = begin + end;
        }
        String randomStr = begin + randomOperation + end + "=";
        redisService.setValueTimeOut(key + type + ":" + phone, String.valueOf(result), 300);
        return randomStr;
    }

}
